package com.r2coding.article.controller;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.r2coding.article.annotation.SysLog;
import com.r2coding.article.vo.req.LabelListReqVo;
import com.r2coding.common.enums.Module;
import com.r2coding.common.enums.OperationType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import com.r2coding.article.entity.LabelEntity;
import com.r2coding.article.service.LabelService;
import com.r2coding.common.util.Result;

/**
 * 标签信息控制器
 *
 * @author 98k灬
 * @email hk666zx@163.com
 * @date 2022-04-13 19:34:16
 */
@RestController
@RequestMapping("/label")
public class LabelController {
    @Autowired
    private LabelService labelService;

    @GetMapping("/getLabelByCategoryId/{categoryId}")
    public Result getLabelByCategoryId(@PathVariable("categoryId") Integer categoryId) {
        QueryWrapper<LabelEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("category_id", categoryId);
        List<LabelEntity> list = labelService.list(wrapper);
        return Result.ok(list);
    }

    /**
     * 保存
     */
    @PreAuthorize("hasAuthority('label:add')")
    @PostMapping("/save")
    public Result save(@RequestBody LabelListReqVo reqVo){
		labelService.addLabel(reqVo);
        return Result.ok();
    }

    /**
     * 修改
     */
    @PreAuthorize("hasAuthority('label:edit')")
    @PutMapping("/update")
    public Result update(@RequestBody LabelListReqVo reqVo){
        labelService.updateLabel(reqVo);
        return Result.ok();
    }

    /**
     * 删除
     */
    //@CacheEvict(value = {"base"}, key = " 'selectBase' ") // 删除缓存
    @SysLog(module = Module.ARTICLE, operationType = OperationType.DELETE, description = "删除标签")
    @PreAuthorize("hasAuthority('label:delete')")
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable("id") Integer id) {
        labelService.removeById(id);
        return Result.ok();
    }
}
